import type { Ref } from "vue";
import type { tablePageTy } from "./type";
import { cloneDeep } from "lodash-unified";
/**
 *
 * @param page 分页
 * @param request table onload请求
 * @param paramsPreFixFn 请求参数前置处理
 * @returns
 * query 缓存的请求参数
 * searchChange 搜索条件变化重新请求
 * searchReset 重置搜索条件
 */
export default function (
  page: Ref<tablePageTy>,
  request: (...args: any) => void,
  paramsPreFixFn?: (params: any) => any
) {
  /*search*/
  const query = ref({});

  const searchChange = (params, done: () => void) => {
    const paramsClone = cloneDeep(params);
    paramsPreFixFn?.(paramsClone);
    query.value = paramsClone;
    page.value.currentPage = 1;
    request(page, paramsClone);
    done();
  };
  const searchReset = () => {
    query.value = {};
    page.value.currentPage = 1;
    request(page, query.value);
  };
  const refreshChange = () => {
    request(page, query.value);
  };
  return {
    query,
    searchChange,
    searchReset,
    refreshChange,
  };
}
